<template>
	<view class="content">
		<image class="logo" src="/static/logo.png"></image>
		<view class="text-area">
			<text class="title">{{title}}</text>
		</view>
		<view v-for="(msg, index) in messages" :key="index">
			<text>{{ msg.topic }}: {{ msg.message }}</text>
		</view>
		<u-button type="error" >危险按钮</u-button>
		<button @click="connectMqttServer">Connect to MQTT Server</button>
		<button @click="publishMqttMessage">Publish Message</button>
		<button @click="subscribe">Subscribe to Topic</button>
		<u-button @click="subscribeA" type="primary">Subscribe TopicABC</u-button>
	</view>
</template>

<script>
	// import {
	// 	connectMqtt,
	// 	publishMessage,
	// 	subscribeTopic,
	// 	disconnectMqtt
	// } from '@/Helper/mqttService.js';

	export default {
		data() {
			return {
				title: 'Hello',
				messages: [],
				connected: false,
				mqttOptions: {
					connectTimeout: 4000, // 超时时间
					clientId: 'uniAppClient', // 客户端ID
					username: 'admin', // 用户名
					password: 'admin123456', // 密码
					keepalive: 60, // 心跳时间
					clean: true, // 清除会话
				},
			}
		},
		onLoad() {
			this.$EmitMsg((topic, message) => {
				this.messages.push({
					topic,
					message
				});
			})
		},
		methods: {
			connectMqttServer() {
				this.$disconnectMqtt()
			},
			handleMqttMessage(topic, message) {
				this.messages.push({
					topic,
					message
				});
			},
			subscribe() {
				this.$subscribeTopic('your/topic');
			},
			subscribeA() {
				this.$subscribeTopic('topicABC');
			},
			publishMqttMessage() {
				const topic = 'topicABC';
				const message = 'Hello MQTT' + Date.now();
				this.$publishMessage(topic,message)
				// if (this.connected) {
				// 	const topic = 'Sub/S1600/2106';
				// 	const message = 'Hello MQTT' + Date.now();
				// 	publishMessage(topic, message);
				// } else {
				// 	console.error('MQTT client is not connected');
				// }
			},
		}
	}
</script>

<style scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>